package cn.zyb.org.service.impl;

import cn.zyb.basic.exception.BusinessException;
import cn.zyb.basic.service.Impl.BaseServiceImpl;
import cn.zyb.basic.util.BaiduAiUtils;
import cn.zyb.basic.util.Md5Utils;
import cn.zyb.basic.util.StrUtils;
import cn.zyb.org.domain.Employee;
import cn.zyb.org.domain.Shop;
import cn.zyb.org.domain.ShopAuditLog;
import cn.zyb.org.domain.ShopDto;
import cn.zyb.org.mapper.EmployeeMapper;
import cn.zyb.org.mapper.ShopAuditLogMapper;
import cn.zyb.org.mapper.ShopMapper;
import cn.zyb.org.service.IShopService;
import cn.zyb.user.domain.Logininfo;
import cn.zyb.user.mapper.LogininfoMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;


/**
 * 业务实现类：店铺
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
  @Autowired
  private ShopMapper shopMapper;
  @Autowired
  private EmployeeMapper employeeMapper;
  @Autowired
  private ShopAuditLogMapper shopAuditLogMapper;
  @Autowired
  private LogininfoMapper logininfoMapper;

  @Autowired
  private JavaMailSender javaMailSender;

  @Override
  public void settlement(Shop shop) {
    String address = shop.getAddress();
    String name = shop.getName();String tel = shop.getTel();
    //一：校验
    //1.校验空值
    if(StringUtils.isEmpty(address) || StringUtils.isEmpty(name) || StringUtils.isEmpty(tel) ){
      throw new BusinessException("数据不能为空");
    }
    //2.校验密码是否一致[思路：在domain中加一个参数就可以接收确认密码]

    //3.校验店铺是否已经入驻过
    Shop dbShop = shopMapper.loadByNameAndAddress(shop);
    if(dbShop != null){//入驻过
      throw new BusinessException("该店铺已入驻，请直接登录。忘记密码请联系管理员!!!");
    }
    //4.智能审核，百度AI
    //审核文本
    if(!BaiduAiUtils.textCensor(shop.getName())){
      throw new BusinessException("店铺名称不合法，请重新输入！");
    }
    //审核图片
    String imgUrl = "http://123.207.27.208" + shop.getLogo();
    if(!BaiduAiUtils.imgCensor(imgUrl)){
      throw new BusinessException("店铺logo非法!!!");
    }
    //二：保存数据
    //1.保存店铺管理员信息
    Employee admin = shop.getAdmin();
    //设置盐值
    String salt = StrUtils.getComplexRandomString(32);
    admin.setSalt(salt);
    String md5Pwd = Md5Utils.encrypByMd5(admin.getPassword() + salt);
    admin.setPassword(md5Pwd);
    //保存logininfo的信息
    Logininfo logininfo = new Logininfo();
    BeanUtils.copyProperties(admin,logininfo);
    logininfo.setType(0);//设置管理员
    logininfoMapper.save(logininfo);

    employeeMapper.save(admin); //这一步执行完就有employee_id
    //2.保存店铺信息
    shop.setAdmin_id(admin.getId());
    shopMapper.save(shop);//这一步执行完就有shop_id
    //3.将shopId设置到employee中
    admin.setShop_id(shop.getId());
    employeeMapper.update(admin);
  }

  @Override
  public void auditPass(ShopAuditLog shopAuditLog) throws MessagingException { //shopId,note
    //1.改状态：1 - 2
    Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
    shop.setState(2);
    shopMapper.update(shop);

    //2.添加审核日志
    shopAuditLog.setState(2);
    shopAuditLog.setAuditId(325L); //写死的，因为现在没有登录，不知道是哪一个管理员审核的
    shopAuditLogMapper.save(shopAuditLog);

    //3.发送激活邮件 @TODO
    //创建复杂的邮件对象
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
    //设置发送方
    helper.setFrom("1757295077@qq.com");
    Employee employee = employeeMapper.loadById(shop.getAdmin_id());
    //设置收件人
    helper.setTo(employee.getEmail());
    //设置主题
    helper.setSubject("店铺激活邮件");
    //设置内容
    helper.setText("<h2>店铺激活邮件<br/><a href='http://localhost:8080/shop/active/"+shop.getId()+"'>点击链接激活</a></h2>",true);
    //发送邮件
    javaMailSender.send(mimeMessage);
  }

  @Override
  public void auditReject(ShopAuditLog shopAuditLog) throws MessagingException {
    //1.改状态：1 - 4
    Shop shop = shopMapper.loadById(shopAuditLog.getShopId());
    shop.setState(4);
    shopMapper.update(shop);

    //2.添加审核日志
    shopAuditLog.setState(4);
    shopAuditLog.setAuditId(325L);
    shopAuditLogMapper.save(shopAuditLog);

    //3.发送邮件重新入驻 @TODO
    //创建复杂的邮件对象
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true, "UTF-8");
    //设置发送方
    helper.setFrom("1757295077@qq.com");
    Employee employee = employeeMapper.loadById(shop.getAdmin_id());
    //设置收件人
    helper.setTo(employee.getEmail());
    //设置主题
    helper.setSubject("店铺审核失败");
    //设置内容
    helper.setText("<h2>你的店铺审核失败，请重新入驻<br/>" +
      "<a href='http://localhost:8081/#/register'>点击链接重新入驻</a></h2>",true);
    //发送邮件
    javaMailSender.send(mimeMessage);

  }

  @Override
  public List<ShopDto> getCountByState() {
    return shopMapper.getCountByState();
  }

  @Override
  public void active(Long id) {
    Shop shop = shopMapper.loadById(id);
    shop.setState(3);
    shopMapper.update(shop);
  }
}
